# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=notcurses
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.0.13
pkgrel=1
pkgdesc="Blingful character graphics/TUI library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://nick-black.com/dankwiki/index.php/Notcurses'
license=('spdx:Apache-2.0')
depends=(
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
  "${MINGW_PACKAGE_PREFIX}-libdeflate"
  "${MINGW_PACKAGE_PREFIX}-libunistring"
  "${MINGW_PACKAGE_PREFIX}-ncurses"
  "${MINGW_PACKAGE_PREFIX}-openimageio"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-doctest"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
)
source=(
  "https://github.com/dankamongmen/notcurses/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
  0001-notcurses-fix-pkgconfig-path.patch
  0002-notcurses-fix-pkgconfig-requires.patch
)
sha256sums=('cf6342e50c02b830869bd28d8c953d22316b942b671e833c0f36899502084b35'
            '9d08f66abd49c7be8f569ed999cc6ee5521bb7fc6452f29dc8ea24e5cabc9a79'
            '67959efe39a72b9c007fc7dacfef68655804e7f79375023dcbed1ccec9d2f7a8')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/0001-notcurses-fix-pkgconfig-path.patch
  patch -p1 -i "${srcdir}"/0002-notcurses-fix-pkgconfig-requires.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DUSE_PANDOC=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

check() {
  cmake --build "build-${MSYSTEM}" --target test || warning "Tests failed"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYRIGHT \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYRIGHT
}
